home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / aecdev.aedaemon / read me ¥ aecdev-aedaemon < prev   
Encoding:
Text File  |  2000-06-23  |  3.6 KB  |  74 lines

  1. AECDEV/AEDaemon
  2. This sample is designed to answer that ago-old question;
  3. "How do I send AppleEvents from a DA/CDEV/INIT/Driver?"
  4. The short answer is, you can't.  The High Level Event manager, which the
  5. AppleEvent manager uses to dispatch AppleEvents, will not allow a 
  6. PostHighLevel event call from CODE that does not have a PPC port and
  7. event loop to recieve the reply back through.  DA/CDEV/INIT code 
  8. does not have an event loop, so they cannot be high level event aware,
  9. ergo they cannot send AppleEvents.  Bad situation.
  10.  
  11. The Solution
  12. The solution is to have a helper do it for you.
  13. This means that instead of one bit of code, you have two.  The 
  14. CDEV (in this case) prepares all the data that will be a part of
  15. the AppleEvent, then passes the data to a faceless background application.
  16. The faceless background application, since it is a 'real' app with an
  17. event loop, can send the AppleEvent.
  18.  
  19. How It Works
  20. When you click the 'Send an odoc event' button in the CDEV, the CDEV
  21. builds an 'odoc' AppleEvent, using AppleEvent Manager calls.
  22. Then it looks for the backgrounder.  It first does an IPCListPorts to
  23. see if the backgrounder's PPC port is currently open.  If it is not,
  24. it searches (using PBCatSearch) for the backgrounder, and launches it
  25. when it finds it, and keeps trying IPCListPorts until the port opens.
  26. When the CDEV has found the backgrounder, it opens a PPC port for itself, then
  27. begins a PPC session with the backgrounder.  The CDEV writes the data 
  28. portion of the AppleEvent it has created to the backgrounder.
  29. The backgrounder reads all the data, when the reading is done, it sends
  30. the AppleEvent using AESend.
  31. Simple.
  32.  
  33. And this sample is simple, all it does is send a reply-less odoc event.
  34. But you can easily build it up to do more.
  35. Currently, the only data that is sent across is the 'odoc' event data.  But,
  36. you can also transfer a 'header' block before the MAVT data block, which could
  37. specify what it is you're sending, if you want a reply, the priority, the interaction 
  38. level, or anything else.  An example would be...
  39.  
  40. struct MyAEvtPPCHeader{
  41. short Interaction;
  42. short reply;
  43. Boolean layerSwitch;
  44. short priority;
  45. short timeOut;
  46. };
  47.  
  48. Currently, the AECDEV closes it's port after it sends the data.  If you wanted a 
  49. reply, all you'd do would be to add a PPCRead after the PPCWrite, and the AEDaemon
  50. would wait for the reply, then send the reply back with a PPCWrite.
  51. Again, I wanted to keep this simple, so I didn't add that section.  But all the 
  52. calls to do it are already here, just re-arrange them to suit your needs.
  53.  
  54. Launching AEDaemon
  55. Every time the AECDEV wants to send an event, it searches for the AEDaemon and
  56. launches it if it can't find it.  This continual launching could cause 
  57. fragmentation of MultiFinder memory, so we recommend that AEDaemon (or whatever
  58. you write) be installed in the StartUp items folder in the system folder.  This way
  59. it will be launched every time the user reboots.
  60. Also, by launching right after the Finder starts, it will be high in MultiFinder's 
  61. heap, so it will not be causing any fragmentation problems.
  62.  
  63. I hope this sample helps you folks who are working with DAs and other non-application
  64. code under system 7.
  65. However, I also strongly recommend that you convert your DAs or CDEVs to small 
  66. applications instead.  Using a helper application is a stopgap, and not the 
  67. most efficient answer.  When you can safely assume that you will be running in
  68. a System 7 world, the benifits of converting to a Application should make it
  69. compelling for you to do so.
  70. Applications have many more rights and priledges than other CODE bits, and with
  71. full-time MultiFInder you should be able to make the conversion, and live a
  72. happier life.
  73. C.K. Haun
  74. Apple DTS